package com.cloud.alibaba.rest.config;

import com.cloud.alibaba.common.page.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Slf4j
public class DefaultExceptionHandler {

	@ExceptionHandler({ Exception.class })
	@ResponseBody
	public BaseResult processUnauthenticatedException(HttpServletRequest request, Exception exception) {
		String servletPath = request.getServletPath(); // 获取请求的URL
		BaseResult failedResult = null;
		String message = BaseResult.getFinalMessage(exception);
		String msg = "请求URL：" + servletPath + "，发生错误：" + message;
		log.error(msg, exception);
		failedResult = new BaseResult(BaseResult.getTruncateMessage(message), exception);
		return failedResult;
	}
}
